<?php
/*
 *@des系统基本信息设置 
 *@author wangjian
 *@time 2013-10-02
 */
if (! defined('BASEPATH')) {
	exit('Access Denied');
}
define("__PATH_ROOT__", str_replace("\\","/",FCPATH));
class Index extends MY_Controller {
	private $varname = array() ;
	private $username ;//当前登录的用户
	private $ip ; //登录的ip地址
	private $table_ ; //表的前缀
	//private $path ="data/sysconfig/sysconfig.txt"; //生成的系统配置信息文件
	//private $path_js = "data/sysconfig/sysconfig.js"; //生成的js文件名字
	private $group_ = array() ;
	//private $js_var = 'sys_config_var' ; //定义生成的js变量
	private $cache_path ; 
	function Index(){
		parent::__construct();
		$this->load->model('admin/web_manager/M_site_config');
		$this->load->library('admin_common');//加载admin的公用方法
		$this->username = $this->get_login_name(); //得到登录的用户名
		$this->load->model('admin/M_log'); //加载日志model
		$this->ip = $this->admin_common->get_client_ip();
		$this->table_ = $this->admin_common->table_pre('real_data');
		
		$this->group_ = array(
			1=>'站点设置',
			2=>'会员设置',
			3=>'性能设置',
			4=>'其他设置',
		);
		$this->cache_path = __PATH_ROOT__."/data/sysconfig/"; //生成系统配置信息缓存目录
	}
	//站点配置页面
	function site_config(){
		$data_ = $_REQUEST ;
		$action_array = array(
			'config','add_config'
		);
		$action = (isset($data_['action']) && in_array($data_['action'],$action_array))?$data_['action']:'config';
		$config_array = $this->varname ;
		if($action == 'config'){
			$sql_search = "SELECT * FROM `{$this->table_}common_sysconfig` order by aid desc " ;
			$list_data = $this->M_site_config->querylist($sql_search);
			$data_t = array();
			if($list_data){
				foreach($list_data as $key=>$value){
					
					if(array_key_exists($value['groupid'], $data_t)){//如果存在
						$data_t[$value['groupid']][]=$value ;
					}else{
						$data_t[$value['groupid']][0] = $value ;
					}
					
				}
			}
			
			$in_ = '' ;//返回一个输入框
			if($data_t){
				foreach($data_t as $data_key=>$data_val){
					foreach($data_val as $data_val_key=>$data_val_val){
						
						if($data_val_val['type'] == 'string'){
							$in_ = "<input type='text' name='{$data_val_val['varname']}' value='{$data_val_val['value']}' style='width:400px;height:30px'>" ;
						}elseif($data_val_val['type'] == 'textarea'){
							$in_ = "<textarea  name='{$data_val_val['varname']}' style='width:400px'>{$data_val_val['value']}</textarea>" ;
						}elseif($data_val_val['type'] == 'boolean'){
							if($data_val_val['value'] == 'Y'){
								$in_ = "是<input type='radio' name='{$data_val_val['varname']}' value='Y' checked=\"checked\">否<input type='radio' name='{$data_val_val['varname']}' value='N' >" ;
							}else{
								$in_ = "是<input type='radio' name='{$data_val_val['varname']}' value='Y' >否<input type='radio' name='{$data_val_val['varname']}' value='N' checked=\"checked\">" ;
							}
							
						}elseif($data_val_val['type'] == 'number'){
							$in_ = "<input type='text' name='{$data_val_val['varname']}' value='{$data_val_val['value']}' style='width:400px'>" ;
						}
					
					$data_t[$data_key][$data_val_key]['in_'] = $in_;
					}
				}
			}
		
			$data['group_'] = $this->group_ ;
			$data['list_'] = $data_t ;
			//echo '<pre>';
			//print_r($list_data); 
			
			$this->load->view("admin/web_manager/views_site_config",$data);
		}elseif($action == 'add_config'){
			$this->add_config();
		}

	}
	
	//处理添加系统设置信息
	private function add_config(){
		$data_ = $post_data = $this->input->post(NULL,true) ;
		$operation = 'add_varname';
		$operation_array = array(
			'add_varname','edit_varname'
		);
		$operation = (isset($data_['operation']) && in_array($data_['operation'],$operation_array))?$data_['operation']:'add_varname';
		if($operation == 'add_varname'){
			$this->add_varname();
			exit();
		}elseif($operation == 'edit_varname'){
			//编辑
			$this->edit_varname();
			die();
		}
	
	}
	//添加新变量
	private function add_varname(){
		
		$varname =  html_escape($this->admin_common->do_addslashes(trim($_REQUEST['varname'])));
		$value =  html_escape($this->admin_common->do_addslashes(trim($_REQUEST['value'])));
		$info =  html_escape($this->admin_common->do_addslashes(trim($_REQUEST['info'])));
		$group_id = intval($_REQUEST['group']);
		$type = html_escape($this->admin_common->do_addslashes(trim($_REQUEST['type'])));
		if($this->admin_common->utf8_str($varname) != 1){
			//如果不是英文
			showmessage('变量名必须是英文','admin/web_manager/index/site_config',3,0);
		}
		if(empty($varname)){
			showmessage('变量名不可以为空','admin/web_manager/index/site_config',3,0);
		}
		if(empty($value)){
			showmessage('变量值不可以为空','admin/web_manager/index/site_config',3,0);
		}
		if(empty($info)){
			showmessage('说明不可以为空','admin/web_manager/index/site_config',3,0);
		}
		$row = $this->M_site_config->querylist("SELECT * FROM `{$this->table_}common_sysconfig` WHERE `varname` = '{$varname}'");
		if($row){
			showmessage("变量名称{$varname}有重复",'admin/web_manager/index/site_config',3,0) ; 
			exit();
		}
		$sql_insert = "INSERT INTO `{$this->table_}common_sysconfig`(`varname`,`value`,`info`,`groupid`,`type`)VALUES('{$varname}','{$value}','{$info}','{$group_id}','{$type}')" ;
		$this->M_site_config->insert_one($sql_insert);
		$this->M_log->insert("{$sql_insert}",'system_setting_add_varname',$this->username,$this->ip,1,"插入变量{$varname}成功"); //插入日志记录
		showmessage('添加成功','admin/web_manager/index/site_config',3,1) ; 
	}
	
	//编辑
	private function edit_varname(){
		$data_ = $post_data = $this->input->post(NULL,true) ;
		$insert_array  = array();
		if($data_){			
			foreach($data_ as $k=>$v){
				$value = html_escape($this->admin_common->do_addslashes(trim($v)));
				if(!in_array($value,array('add_config','edit_varname'))){
					$insert_array[$k] = $value ;	
				}
				
			}	
				
		}
		
		if($insert_array){
			foreach($insert_array as $last_key=>$last_val){

				$sql_ = "UPDATE `{$this->table_}common_sysconfig` SET `value` = '{$last_val}' WHERE `varname` = '{$last_key}'";
				$this->M_site_config->update_data($sql_);
				$this->M_log->insert($sql_,'system_setting_edit_varname',$this->username,$this->ip,1,"修改变量{$last_key}为{$last_val}成功"); //插入日志记录
			}	
		}else{
			$this->M_log->insert("no_system_insert_sql",'system_setting_edit_varname',$this->username,$this->ip,0,"修改信息失败,可能数据没有变化"); //插入日志记录
		}	
		
		
		showmessage('修改成功','admin/web_manager/index/site_config',3,1) ; 
	}
	
	//删除变量
	 function del(){
		$data_ =  $this->input->get(NULL,true) ;
		$value = html_escape($this->admin_common->do_addslashes(trim($data_['varname'])));
		if(empty($value)){
			showmessage('删除出错，变量名称不可以为空','admin/web_manager/index/site_config',3,0);
			exit();
		}
		$sql_del = "DELETE  FROM `{$this->table_}common_sysconfig` WHERE `varname` = '{$value}'";
		$num = $this->M_site_config->del_data($sql_del);
		if($num >= 1){
			$this->M_log->insert($sql_del,'system_setting_del_varname',$this->username,$this->ip,1,"删除变量{$value}成功"); //插入日志记录
			showmessage('删除成功','admin/web_manager/index/site_config',3,1) ; 			
			exit();
		}else{
			$this->M_log->insert($sql_del,'system_setting_del_varname',$this->username,$this->ip,0,"删除变量{$value}失败"); //插入日志记录
			showmessage('删除出错，服务器繁忙','admin/web_manager/index/site_config',3,0);
			
			exit();
		}
	}
	//生成数据文件
	function make_data(){
			$sql_search = "SELECT * FROM `{$this->table_}common_sysconfig` " ;
			$list_data = $this->M_site_config->querylist($sql_search);
			$path =str_replace("\\", "/", FCPATH);
			if(!is_really_writable(__PATH_ROOT__."/admin_application/cache")){
				showmessage("目录".__PATH_ROOT__."/admin_application/cache不可写，请修改目录权限",'admin/web_manager/index/site_config',3,0);
				exit();
			}
			if(!file_exists($this->cache_path)){
				mkdir($this->cache_path);
			}
			$configfile = $this->cache_path."cache_system.inc.php";
			$fp = fopen($configfile,'w');
			flock($fp,3);
			fwrite($fp,"<"."?php\r\n");
			fwrite($fp,"/*网站基本信息配置*/\r\n");
			fwrite($fp,"/*author wangjian*/\r\n");
    		fwrite($fp,"/*time 2013_12_08*/\r\n");
    		
			
			if($list_data){
				foreach($list_data as $j_key=>$j_val){
					$value = $this->admin_common->do_addslashes($j_val['value']);
					if($j_val['type'] == 'number'){
						$value = intval($j_val['value']);
					}
					
					fwrite($fp,"\${$j_val['varname']} ='{$value}';\r\n");
				}
			}
	
		
			$this->M_log->insert("no_system_make_data_sql",'system_setting_make_data',$this->username,$this->ip,1,"生成系统基本参数缓存文件成功"); //插入日志记录
			$json_data = array() ;
			echo $this->admin_common->result_to_towf_new($json_data,1,"生成成功,生成的文件是在{$configfile}",'');
			
			
	}
	
}
